import { CurrencyPipe } from '@angular/common';
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { DateHelperByDatePipe } from 'ng-zorro-antd/i18n';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { AddCollectionInvoiceModalComponent } from 'src/app/routes/ticket-management/components/input-invoice/add-collection-invoice-modal/add-collection-invoice-modal.component';
import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-voucher-summary',
  templateUrl: './voucher-summary.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class VoucherSummaryComponent extends BasicTableComponent implements OnInit {
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  selectedRows: any[] = [];
  constructor(
    public service: FreightAccountService,
    private nzModalService: NzModalService,
    private router: Router,
    private dateHelperByDatePipe: DateHelperByDatePipe,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, { isvc2: 1 });
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value,
        createtime: {
          start: this.sf?.value.createtime?.[0] || '',
          end: this.sf?.value.createtime?.[1] || ''
        },
        vctime: {
          start: this.sf?.value.vctime?.[0] || '',
          end: this.sf?.value.vctime?.[1] || ''
        }
      });
    }
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  addInvoice() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择申请记录');
      return;
    }
    const modal = this.nzModalService.create({
      nzTitle: '收票信息',
      nzContent: AddCollectionInvoiceModalComponent,
      nzComponentParams: { i: { userId: 0 } },
      nzFooter: null
    });
  }

  exportList() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_export_fico_vch_page);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        vc2code: {
          type: 'string',
          title: '汇总凭证号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        cno: {
          type: 'string',
          title: '客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: ''
        },
        vctime: {
          title: '凭证时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        sourceCode: {
          type: 'string',
          title: '原始单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        sourceType: {
          type: 'string',
          title: '原始单类型',
          ui: {
            widget: 'select',
            placeholder: '请选择'
          }
        },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        remarks: {
          type: 'string',
          title: '凭证摘要',
          ui: {
            placeholder: '请输入'
          }
        },
        vctype: {
          type: 'string',
          title: '凭证类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'credential:type' },
            placeholder: '请选择'
          },
          default: ''
        },
        s2ts: {
          type: 'string',
          title: '科目',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择'
          }
        },
        drmoney: {
          type: 'string',
          title: '借方金额',
          ui: {
            placeholder: '请输入'
          }
        },
        crmoney: {
          type: 'string',
          title: '贷方金额',
          ui: {
            placeholder: '请输入'
          }
        },
        vcltdid: {
          type: 'string',
          title: '帐套',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择'
          }
        },
        sts: {
          type: 'string',
          title: '凭证状态',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'credential:status' },
            placeholder: '请选择'
          }
        },
        feecode: {
          type: 'string',
          title: 'NC凭证',
          ui: {
            placeholder: '请输入'
          }
        },
        vccode: {
          type: 'string',
          title: '凭证号',
          ui: {
            placeholder: '请输入'
          }
        },
        s22t2ss: {
          type: 'string',
          title: '导入NC',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择'
          }
        },
        createt1im2e: {
          title: '统计时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox', width: 60, className: 'text-center', fixed: 'left' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '汇总凭证号', index: 'vc2code', type: 'link', width: 200 },
      { title: '帐套', index: 'vcltdcode', width: 180 },
      { title: '凭证时间', index: 'createTime', type: 'date', width: 150 },
      {
        title: '统计区间',
        index: 'invdate',
        type: 'date',
        width: 200,
        format: item =>
          `${this.dateHelperByDatePipe.format(item.vctime2start, 'yyyy-MM-dd')} | ${this.dateHelperByDatePipe.format(
            item.vctime2end,
            'yyyy-MM-dd'
          )}`
      },
      { title: '凭证类型', index: 'vctype', width: 120 },
      { title: '序号', index: 'invmoney', width: 100, format: _ => '1' },
      { title: '摘要', index: 'remarks', width: 220 },
      { title: '币种', index: 'currency', width: 100 },
      {
        title: '借方金额',
        index: 'drmoney',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.drmoney }) }
      },
      {
        title: '贷方金额',
        index: 'crmoney',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.crmoney }) }
      },
      { title: 'NC凭证', index: 'importncnotes', width: 180 },
      { title: '凭证状态', index: 'stsLabel', width: 120 },
      { title: '创建时间', index: 'createTime', width: 180 },
      { title: '创建人', index: 'createUserName', width: 120 },
      {
        title: '操作',
        width: '130px',
        fixed: 'right',
        className: 'text-center',
        buttons: [
          {
            text: '浏览',
            acl: { ability: ['FINANCIAL-VOUCHER-SUMMARY-view'] },
            click: (item: any) => this.router.navigate(['/financial-management/voucher-summary/detail/' + item.id])
          },
          {
            text: '列表',
            acl: { ability: ['FINANCIAL-VOUCHER-SUMMARY-child-list'] },
            click: (item: any) => this.router.navigate(['/financial-management/voucher-summary/list/' + item.vc2code])
          }
        ]
      }
    ];
  }
}
